// pages/xiadan/xiadan.js
var app = getApp()
var url = app.globalData.url
var url1 = app.globalData.url1
var address = {}

var menuName1 = ""
var menuName2 = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {

    mainmenus: [{}, {}, {}, {}, {
      menuId: 0,
      menuIcon: '',
      menuName: "常用服务"
    }],
    secmenus: [0, 0],
    names: "一键快修",
    flag: false,
    toolIndex: 0,
    showPop: true,
    location: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this
    that.getMenuList()
  },

  getpictures(menuid) {
    const that = this
    wx.request({
      url: url + '/menu/menus_icon',
      data: {
        mid: menuid
      },
      header: {
        "content-type": "application/json"
      },
      success(res) {
        console.log("查询图片")
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            imgs: res.data.data.icon
          })
        }
      }

    })
  },
  phoneCat() {
    wx.navigateTo({
      url: "../equipment/equipment?page=myaddr&index=4&selectAddress=true"
    })
  },
  selectMenu(e) {
    console.log(e)
    menuName2 = e.currentTarget.dataset.name
    this.getpictures(e.currentTarget.dataset.id)
    this.setData({
      menuIndex:e.currentTarget.dataset.index
    })
  },
  getMenuList() {
    const that = this
    wx.request({
      url: url + '/menu/menus_one',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log("获取一级菜单：")
        console.log(res.data)

        if (res.data.code == 1) {
          res.data.data.menus.push({
            menuId: 0,
            "menuIcon": {
              "menuIconChecked": "../image/icon05.png",
              "menuIconUnChecked": "../image/icon05.png",
              "firstColor": "",
              "lastColor": ""
            },
            menuName: "常用服务"
          })
          menuName1=res.data.data.menus[0].menuName
          that.setData({
            mainmenus: res.data.data.menus
          })
          console.log("赋值后的mainmenus：")
          //  console.log(this.data.mainmenus)
          that.getsecond(res.data.data.menus[0].menuId)
        } else {
          wx.showToast({
            title: '加载失败',
            icon: "loading",
            duration: 1000
          })
        }

      }

    })
  },
  getsecond(data) {
    const that = this

    wx.request({
      url: url + '/menu/menus_pid',
      data: {
        pid: data
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log("获取二级菜单：")
        console.log(res.data)

        if (res.data.code == 1) {
            menuName2=res.data.data.menus[0].menuName
          that.getpictures(res.data.data.menus[0].menuId)
          that.setData({
            secmenus: res.data.data.menus,
            menuIndex:0
          })
        } else {
          that.setData({
            secmenus: []
          })
          wx.showToast({
            title: '暂无数据',
            icon: "loading",
            duration: 1000
          })
        }
      }
    })
  },
  // 常用服务
  getUral() {
    const that = this
    wx.request({
      url: url + '/menu/menu_commonly_used',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log("获取常用服务：")
        console.log(res.data)

        if (res.data.code == 1) {
          that.setData({
            menuUral: res.data.data.menu
          })
        } else {
          wx.showToast({
            title: '加载失败',
            icon: "loading",
            duration: 1000
          })
        }

      }

    })
  },
  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },
  tosecMenu(e) {
    var id = e.currentTarget.id
    const that = this
    menuName1 = e.currentTarget.dataset.name
    that.getsecond(id)
    that.sildeOut()
    setTimeout(function() {
      that.fadeOut()
    }, 500)
    that.setData({
      toolIndex: e.currentTarget.dataset.index
    })

  },
  getMenus(e) {
    menuName1 = e.currentTarget.dataset.name1
    menuName2 = e.currentTarget.dataset.name2

    this.popupTap()
  },
  showPop() {
    this.showPopup()

    this.setData({
      showPop: true,
      menuNames:"常用服务"
    })
    this.getUral()
  },
  showPop2() {
    this.showPopup()
    this.setData({
      showPop: false,
      menuNames: menuName1 + menuName2
    })
  },
  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    /*popupComponent && popupComponent.show();*/
    popupComponent && popupComponent.toggle(true);
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    //popupComponent && popupComponent.hide();
    popupComponent && popupComponent.toggle();
  },
  // 菜单事件
  sildeOut() {
    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: "liner",
      delay: 0
    })
    this.animation = animation
    animation.translateX(800).opacity(0).step().translateX(-800).step()
    this.setData({
      animation: animation.export(),
    })
  },
  fadeOut() {
    var animation = wx.createAnimation({
      duration: 100,
      timingFunction: "liner",
      delay: 0
    })
    this.animation = animation
    animation.opacity(1).translateX().step()
    this.setData({
      animation: animation.export(),
    })

  },
  xiadan() {
    const that = this
    var user=wx.getStorageSync("user")
    wx.showLoading({
      title: '正在下单',
    })
    console.log("下单:")
    console.log(address.addressDoorNumber)
    wx.request({
      url: url1+'/orders/yiJIan',
      data: {
        usesAccount: user.wxUsersAccount,
        addressName:address.addressName,
        addressTel:address.addressPhone,
        addressLocal: address.addressLocation + address.addressDoorNumber,
        longitude: address.addressLongitude,
        latitude: address.addressLatitude,
        Service:menuName2+menuName1

      },
      header: {
        "contentType": "application/json"
      },
      success(res) {
        if (res.data.code === 1) {
          wx.hideLoading()
          wx.setStorageSync('address', "")
          app.globalData.currentTab=4
          wx.navigateTo({
            url: '../order/order',
          })
        }
      },
    })
  },
  selectAddr() {
    var that = this;

    wx.getSetting({
      success: (res) => {
        console.log(res);
        console.log(res.authSetting['scope.userLocation']);
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则下单等功能将无法使用',
            success: function(res) {
              if (res.cancel) {
                console.info("授权失败");

              } else if (res.confirm) {
                //village_LBS(that);
                wx.openSetting({
                  success: function(data) {
                    console.log(data);
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      //再次授权，调用getLocationt的API

                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            }
          })
        }
      }
    })

    wx.chooseLocation({
      success: function(s) {
        console.log(s)
        addr.addressLocation = s.address
        longitude = s.longitude
        latitude = s.latitude
        that.setData({
          location: s.address
        })
      },
    })

  },
  getName(e) {
    console.log(e)
    address.addressName = e.detail.value
  },
  getPhone(e) {
    address.addressPhone = e.detail.value
  },
  getAddrDoornumber(e) {
    address.addressDoorNumber = e.detail.value
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.editTabBar()
    const that = this
    var address1 = wx.getStorageSync("address")
    console.log("onshow")
    console.log(address)
    if (address1.addressDoorNumber != undefined) {
      address = address1
      that.setData({
        addressName: address.addressName,
        addressTel: address.addressPhone,
        location: address.addressLocation
      })
    } else {
      that.getDefaultAddr()
    }
  },
  // 获取默认地址
  getDefaultAddr(id) {
    console.log(id)
    const that = this
    var user = wx.getStorageSync("user")
    wx.request({
      url: url + '/address/default_address',
      method: "get",
      header: {
        "content-type": "application/json"
      },
      data: {
        usersTel: user.wxUsersAccount
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 1) {
          address = res.data.data.address
          that.setData({
            addressName: address.addressName,
            addressTel: address.addressPhone,
            location: address.addressLocation
          })
        } else {

        }
      }
    })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})